package com.timothy.junk.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.timothy.junk.R;
import com.timothy.junk.common.util.SPUtil;
import com.timothy.junk.gfund.bean.GFundNotifyBean;

import java.util.List;

public class GFundSettingAdaptor extends BaseAdapter {
	
	public static final String LOG_TAG = "GFundSettingAdaptor";
	
	public static final float MAX_CEIL = 8.0f;
	public static final float MIN_FLOOR = 1.0f;
	public static final float GRAD = 0.5f;
	
	private Context 					mContext;
	private SharedPreferences sp;
	private List<GFundNotifyBean> 		listItems;
	private LayoutInflater 				mInflater;

	static class ViewHolder{
		 public TextView settingIndex;
		 public TextView fundName;
		 public EditText ceilDiscount;
		 public ImageButton addCeil;
		 public EditText floorDiscount;
		 public ImageButton addFloor;
		 public ToggleButton isNotify;
	}

	public GFundSettingAdaptor(Context context, List<GFundNotifyBean> data,SharedPreferences sp) {
		this.mContext = context;
		this.sp = sp;
		mInflater = LayoutInflater.from(context); 
		this.listItems = data;
	}
	
	@Override
	public int getCount() {
		return listItems.size();
	}

	@Override
	public Object getItem(int arg0) {
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		ViewHolder holder;  
        if (convertView == null){  
            holder = new ViewHolder();  
            convertView = mInflater.inflate(R.layout.gfund_setting_list_item, null);
            
            holder.settingIndex = (TextView) convertView.findViewById(R.id.settingIndex);
			holder.fundName = (TextView) convertView.findViewById(R.id.fundName);
			
			holder.ceilDiscount = (EditText) convertView.findViewById(R.id.ceilDiscount);
			holder.addCeil = (ImageButton) convertView.findViewById(R.id.addCeil);
			
			holder.floorDiscount = (EditText) convertView.findViewById(R.id.floorDiscount);
			holder.addFloor = (ImageButton) convertView.findViewById(R.id.addFloor);
			
			holder.isNotify = (ToggleButton) convertView.findViewById(R.id.isNotify);
			
            convertView.setTag(holder);  
        }else {  
            holder = (ViewHolder) convertView.getTag();  
        }

        final int p = position;
        GFundNotifyBean bean = listItems.get(position);
//        final float currCeil = bean.getCeilDiscount();
//        float currFloor = bean.getFloorDiscount();
        
        holder.settingIndex.setText(String.valueOf(position+1));
		holder.fundName.setText(bean.getFundMName());
		
		holder.ceilDiscount.setText(String.valueOf(bean.getCeilDiscount()));
		holder.addCeil.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				changeCeil(p);
			}
		});
		
		holder.floorDiscount.setText(String.valueOf((-1.0f) * bean.getFloorDiscount()));
		holder.addFloor.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				changeFloor(p);
			}
		});
		
		if(bean.isNotified()){
			holder.isNotify.setChecked(true);
		}else{
			holder.isNotify.setChecked(false);
		}
		
		holder.isNotify.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				ToggleButton tb = (ToggleButton)v;
				changeNotify(p,tb.isChecked());
			}
		});

        return convertView; 
        
	}

	public void changeCeil(int pos){
		Log.i(LOG_TAG,"changeCeil():"+String.valueOf(pos));
		GFundNotifyBean bean = listItems.get(pos);
		Log.i(LOG_TAG,String.valueOf(bean.getIndex())+":"+String.valueOf(bean.getCeilDiscount()));
		bean.setCeilDiscount(addNotfiy(bean.getCeilDiscount()));
		listItems.set(pos, bean);
		notifyDataSetChanged();
		
		//save to sp
		SPUtil.save2SP(bean.getIndex(), bean.toSPString(), sp);
		
		Log.i(LOG_TAG,"changeCeil end.");
	}
	
	public void changeFloor(int pos){
		Log.i(LOG_TAG,"changeFloor():"+String.valueOf(pos));
		GFundNotifyBean bean = listItems.get(pos);
		bean.setFloorDiscount((-1.0f) * addNotfiy(bean.getFloorDiscount()*(-1.0f)));
		listItems.set(pos, bean);
		notifyDataSetChanged();
		
		//save to sp
		SPUtil.save2SP(bean.getIndex(), bean.toSPString(), sp);
		
		Log.i(LOG_TAG,"changeFloor end.");
	}
	
	public float addNotfiy(float oldValue){
		return (oldValue<MAX_CEIL)?oldValue+GRAD:MIN_FLOOR;
	}
	
	public void changeNotify(int pos,boolean flag){
		Log.i(LOG_TAG,"changeFloor():"+String.valueOf(pos));
		GFundNotifyBean bean = listItems.get(pos);
		bean.setNotified(flag);
		listItems.set(pos, bean);
		notifyDataSetChanged();
		
		//save to sp
		SPUtil.save2SP(bean.getIndex(), bean.toSPString(), sp);
		
		Log.i(LOG_TAG,"changeFloor end.");
	}
	
}
